<!--
 * @author  dumeng
-->
<template>
  <div class="question-panduan">
    <el-form
      ref="panduanform"
      :model="baseform"
      :rules="baserules"
      class="dataForm nomt"
      label-position="right"
      label-width="100px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="题干" prop="stem">
            <el-input
              v-model.trim="baseform.stem"
              :rows="2"
              :disabled="disabled"
              type="textarea"
              placeholder="题干这里填写题目描述"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="答案" prop="answer">
            <el-radio-group v-model="baseform.answer" :disabled="$parent.$route.name !== 'exercisesManage' && !canSelectRa">
              <el-radio label="对" />
              <template v-if="canSelectRa">
                <span v-if="rightA == '对'" class="option-rightasnwer">
                  <i class="el-icon-check" />原正确答案
                </span>
              </template>
              <el-radio label="错" />
              <template v-if="canSelectRa">
                <span v-if="rightA == '错'" class="option-rightasnwer">
                  <i class="el-icon-check" />原正确答案
                </span>
              </template>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="!disabled" :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input v-model.trim="baseform.sort" placeholder="请输入排序号" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="试题解析">
            <el-input
              v-model.trim="baseform.questionAnalysis"
              :rows="2"
              :disabled="disabled"
              type="textarea"
              placeholder="添加试题解析，便于更好的理解答案"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { number } from '@/utils/validate'

export default {
  name: 'Panduan',
  props: {
    type: {
      type: String,
      default: '30'
    },
    getData: {
      type: Object,
      default: () => { }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    toggleRightAnswer: { // 是否可以切换正确答案
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      baseform: {
        stem: '',
        answer: '',
        sort: '',
        questionAnalysis: ''
      },
      baserules: {
        stem: [
          { required: true, message: '请输入题干', trigger: 'blur' }
        ],
        answer: [
          { required: true, message: '请选择答案', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          { validator: number, trigger: 'blur' }
        ]
      },
      rightA: '',
      canSelectRa: false
    }
  },
  watch: {
    toggleRightAnswer(val) {
      this.canSelectRa = val
    }
  },
  created() {
    if (this.getData && this.getData.id) {
      try {
        if (this.getData.question && this.getData.dealType) { // 错题回显返回的数据
          this.$set(this.baseform, 'stem', this.getData.question.stem)
          this.$set(this.baseform, 'sort', this.getData.question.sort)
          this.$set(this.baseform, 'questionAnalysis', this.getData.question.questionAnalysis)
          const answer = this.getData.dealType == 10 ? JSON.parse(this.getData.question.answer) : JSON.parse(this.getData.answer)
          this.$set(this.baseform, 'answer', answer.answers[0])
          this.$set(this, 'rightA', JSON.parse(this.getData.question.answer).answers[0])
          this.canSelectRa = this.getData.dealType != 10
        } else {
          const answer = JSON.parse(this.getData.answer)
          this.$set(this.baseform, 'stem', this.getData.stem)
          this.$set(this.baseform, 'sort', this.getData.sort)
          this.$set(this.baseform, 'questionAnalysis', this.getData.questionAnalysis)
          this.$set(this.baseform, 'answer', answer.answers[0])
          this.$set(this, 'rightA', answer.answers[0])
        }
      } catch (error) {
        this.$message({
          type: 'error',
          message: '参数异常，请重试!',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {
    getPostData() {
      let params = ''
      this.$refs.panduanform.validate(valid => {
        if (valid) {
          const answer = {
            type: this.type,
            flags: [],
            answers: [this.baseform.answer]
          }
          params = {
            answer: JSON.stringify(answer),
            stem: this.baseform.stem,
            sort: this.baseform.sort,
            questionNumber: 1,
            questionAnalysis: this.baseform.questionAnalysis
          }
        }
      })
      return params
    }
  }
}
</script>

<style lang="scss">
.question-panduan {
  .el-radio-group {
    width: 100%;
  }
  .options .el-form-item__content {
    display: flex;
    align-items: center;
    .el-input,
    .el-checkbox,
    .el-radio {
      margin-right: 20px;
    }
  }
  .el-radio {
    margin-right: 10px;
  }
  .option-rightasnwer {
    display: inline-block;
    flex-shrink: 0;
    font-size: 14px;
    color: green;
    margin-right: 20px;
  }
  .order-fixed {
    margin: 10px 0;
    padding-left: 100px;
  }
  .remove-option {
    cursor: pointer;
  }
  .add-optiopn {
    margin-left: 100px;
  }
  .tip {
    padding-left: 100px;
    padding-bottom: 10px;
    color: #666;
  }
}
</style>
